package com.lysj.merchant.merchant.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lysj.admin.utils.constant.CommonConstant;
import com.lysj.admin.utils.exception.BaseException;
import com.lysj.admin.utils.spring.foundation.base.BaseService;
import com.lysj.admin.utils.spring.foundation.pagination.PageUtil;
import com.lysj.admin.utils.spring.foundation.pagination.PageVo;
import com.lysj.admin.utils.web.ParamUtil;
import com.lysj.admin.utils.web.Resp;
import com.lysj.admin.utils.web.SelectItem;
import com.lysj.merchant.management.domain.User;
import com.lysj.merchant.management.repository.UserRepository;
import com.lysj.merchant.management.service.UserService;
import com.lysj.merchant.merchant.domain.Store;
import com.lysj.merchant.merchant.repository.StoreRepository;
import com.lysj.merchant.merchant.vo.StoreUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author Created by wtl on 2019-03-12 17:25:12
 * @description 门店服务层
 */
@Slf4j
@Service
@Transactional
public class StoreService implements BaseService<Store> {

    @Resource
    private StoreRepository storeRepository;

    @Resource
    private UserService userService;

    @Override
    public StoreRepository getRepository() {
        return storeRepository;
    }

    /**
     * @author Created by wtl on 2019/3/19 14:41
     * @Description 商户添加门店，需要获取merchantId
     */
    public String add(String userId, Store entity) {
        // 获取merchantId，只有商户能添加门店，根据商户登录用户找到merchantId
        User user = userService.findOne(userId);
        entity.setMerchantId(user.getMerchantId());
        storeRepository.save(entity);
        return "添加成功";
    }

    /**
     * @author Created by wtl on 2019/3/13 18:47
     * @Description 根据门店id获取门店信息
     */
    public Map<String, Object> findByStoreId(String userId, PageVo pageVo) {
        Map<String, Object> map;
        Pageable pageable = PageUtil.initPage(pageVo);
        User user = userService.findOne(userId);
        // 商户获取门店列表
        if (user.getUserType() == 1) {
            // 先获取商户的门店列表
            Page<StoreUser> storeUsers = storeRepository.findStoreByMerchantId(user.getMerchantId(), pageable);
            for (StoreUser storeUser : storeUsers) {
                // 获取门店的店长
                List<User> users = userService.getRepository().findByStoreIdAndUserTypeAndDelFlag(storeUser.getId(), 2, CommonConstant.NORMAL_FLAG);
                storeUser.setUsername((users.size() > 0) ? users.get(0).getUsername() : "");
            }
            map = mapInit(storeUsers.getTotalPages(), storeUsers.getTotalElements(), storeUsers.getContent());
            return map;
        }
        Store store = storeRepository.findOne(user.getStoreId());
        StoreUser storeUser = new StoreUser();
        CopyOptions copyOptions = CopyOptions.create();
        copyOptions.setIgnoreNullValue(true);
        BeanUtil.copyProperties(store, storeUser, copyOptions);
        List<StoreUser> storeUsers = new LinkedList<>();
        // 店长获取门店
        if (user.getUserType() == 2) {
            storeUser.setUsername(user.getUsername());
            storeUsers.add(storeUser);
            map = mapInit(1, 1, storeUsers);
            return map;
        }
        // 店员获取门店
        List<User> users = userService.getRepository().findByStoreIdAndUserTypeAndDelFlag(storeUser.getId(), 2, CommonConstant.NORMAL_FLAG);
        storeUser.setUsername((users.size() > 0) ? users.get(0).getUsername() : "");
        storeUsers.add(storeUser);
        map = mapInit(1, 1, storeUsers);
        return map;
    }

    /**
     * @author Created by wtl on 2019/3/18 16:48
     * @Description 根据登录用户获取门店下拉框
     */
    public List<SelectItem> storeSelect(String userId) {
        // 获取当前登录用户，判断用户类型
        User user = userService.findOne(userId);
        // 登录的是商户管理员，获取门店列表
        if (user.getUserType() == 1) {
            return storeRepository.findMerchantStore(userId);

        }
        // 登录的是店长或店员，获取对应的门店
        if (user.getUserType() == 2 || user.getUserType() == 3) {
            return storeRepository.findStore(userId);
        }
        return null;
    }

    /**
     * @author Created by wtl on 2019/3/19 11:31
     * @Description 获取登录用户类型
     */
    public Integer findUserType(String userId) {
        User user = userService.findOne(userId);
        return user.getUserType();
    }


    private Map<String, Object> mapInit(Integer totalPages, long totalElements, Object content) {
        Map<String, Object> map = new HashMap<>();
        map.put("totalPages", totalPages);
        map.put("totalElements", totalElements);
        map.put("content", content);
        return map;
    }
}